<template lang="html">
  <div class="main-page" id="store-page">
    <nav-bar :title="title"  @goBack="onClickLeft"/>
    <div :class="'novice ' + pageType">
      <div :class="pageType + ' banner'"></div>
      <div class="prdList">
        <van-list v-model="loading" error-text="请求失败，点击重新加载" @load="onLoad()" loading-text="正在加载中" :finished="finished">
          <van-row gutter="10">
            <van-col span="12" v-for="(i, key) in prd_list" v-bind:key="key">
              <Item :data="i" :user_info="user_info" radius="3" :is_hide_vip="true"></Item>
            </van-col>
          </van-row>
        </van-list>
      </div>
    </div>
  </div>
</template>

<script>
import NavBar from '@/components/global/nav-bar.vue'
import Item from '@/components/active/prd_item.vue'
import Tool from '@/utils/tool.js'

export default {
  components: { NavBar, Item },
  data () {
    return {
      pageType: '',
      title: '',
      prd_list: [],
      finished: false,
      loading: true,
      show: true,
      datatype: '',
      page: 0,
      user_info: {}
    }
  },
  created () {
    // getProfile()
    this.getProfile()
    let pageType = this.$route.params.page
    this.pageType = pageType
    let isClient = Tool.isClient()
    switch (pageType) {
      case 'discount':
        this.title = '会员福利'
        this.datatype = 'new'
        document.title = '会员福利'
        if (isClient) {
          window.location.href = 'yggtitle://' + encodeURIComponent('会员福利')
        }
        break
      case 'price':
        this.title = '新手专享低价'
        this.datatype = 'limit'
        document.title = '新手专享低价'
        if (isClient) {
          window.location.href = 'yggtitle://' + encodeURIComponent('新手专享低价')
        }
        break
      default:
    }
    this.getList()
  },
  methods: {
    onClickLeft () {
      this.$router.go(-1)
    },
    getProfile () {
      return new Promise((resolve, reject) => {
        this.$Axios({ url: 'profile', method: 'get' }).then((resp) => { // 请求成功
          if (resp.return_code === '0000') {
            this.user_info = resp.data.user_info
          }
          resolve(resp)
        }).catch((error) => { // 请求失败
          this.$_requestInterceptor('error', error)
          reject(error)
        })
      })
    },
    getList () {
      var page = this.page
      return new Promise((resolve, reject) => {
        this.$Axios({ url: 'index_activity', params: { page: page, page_size: 10, category_type: this.datatype }, method: 'get' }).then((resp) => { // 请求成功
          if (resp.return_code === '0000') {
            this.prd_list = this.prd_list.concat(resp.data.data)
            this.top_prd = resp.data.top
            this.backgroundimage = resp.data.banner_url
            this.backgroundcolor = resp.data.background_color
            if (resp.data.data.length < 10) {
              this.finished = true
            }
          }
          this.loading = false
          resolve(resp)
        }).catch((error) => { // 请求失败
          this.$_requestInterceptor('error', error)
          reject(error)
        })
      })
    },
    onLoad () {
      this.page = this.page + 1
      this.getList()
    }
  }
}
</script>

<style lang="css" scoped>
</style>
